package com.icloud.admin.webservice.service;



import com.icloud.admin.query.AccessQueryService;
import com.icloud.admin.query.IdentityQueryService;
import com.icloud.admin.query.dto.MenuDTO;
import com.icloud.admin.query.dto.ResourceDTO;
import com.icloud.admin.query.dto.UserDTO;
import com.icloud.admin.query.mapper.MenuDTOMapper;
import com.icloud.admin.query.mapper.ResourceDTOMapper;
import com.icloud.api.vo.authority.PermissionInfo;
import com.icloud.api.vo.user.UserInfo;
import com.icloud.auth.client.jwt.UserAuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class PermissionService {

    @Autowired
    private IdentityQueryService identityQueryService;
    @Autowired
    private AccessQueryService accessQueryService;

    @Autowired
    private MenuDTOMapper menuDTOMapper;

    @Autowired
    private ResourceDTOMapper resourceDTOMapper;

    @Autowired
    private UserAuthUtil userAuthUtil;

    public final static String RESOURCE_TYPE_MENU = "menu";
    public final static String RESOURCE_TYPE_BTN = "button";

    public final static String RESOURCE_REQUEST_METHOD_GET = "GET";
    public final static String RESOURCE_REQUEST_METHOD_PUT = "PUT";
    public final static String RESOURCE_REQUEST_METHOD_DELETE = "DELETE";
    public final static String RESOURCE_REQUEST_METHOD_POST = "POST";

    public final static String RESOURCE_ACTION_VISIT = "访问";

    public UserInfo getUserByUsername(String username) {
        UserInfo info = new UserInfo();
        UserDTO user = identityQueryService.findUserByUsername(username);
        info.setUsername(user.getUsername());
        info.setName(user.getUsername());
        info.setPassword(user.getPassword());
        info.setId(user.getId());
        return info;
    }

    /**
     * 获取所有权限
     *
     * @return 权限列表
     */
    public List<PermissionInfo> getAllPermission() {
        List<PermissionInfo> result = new ArrayList();
        List<MenuDTO> menuList = menuDTOMapper.findAll();

        PermissionInfo info = null;
        menu2permission(menuList, result);

        List<ResourceDTO> resourceList = resourceDTOMapper.findAll();
        resource2permission(result, resourceList);
        return null;
    }

    /**
     * 菜单转权限信息
     *
     * @param menus
     * @param result
     */
    private void menu2permission(List<MenuDTO> menus, List<PermissionInfo> result) {
        PermissionInfo info;

        for (MenuDTO menu : menus) {

            String uri = menu.getHref();
            if (!uri.startsWith("/")) {
                uri = "/" + uri;
            }
            info = new PermissionInfo();
            info.setCode("code");
            info.setType(RESOURCE_TYPE_MENU);
            info.setName(RESOURCE_ACTION_VISIT);
            info.setUri(uri);
            info.setMethod(RESOURCE_REQUEST_METHOD_GET);
            result.add(info);
            info.setMenu(menu.getName());
        }
    }

    public List<PermissionInfo> getPermissionByUsername(String username) {
        UserDTO user = identityQueryService.findUserByUsername(username);
        List<PermissionInfo> result = new ArrayList();

        List<MenuDTO> menuList = menuDTOMapper.findByUserId(user.getId());

        PermissionInfo info = null;
        menu2permission(menuList, result);

        List<ResourceDTO> resources = resourceDTOMapper.findByUserId(user.getId());
        resource2permission(result, resources);
        return result;
    }

    /**
     * 访问元素转权限信息
     *
     * @param result
     * @param resources
     */
    private void resource2permission(List<PermissionInfo> result, List<ResourceDTO> resources) {
        PermissionInfo info;
        for (ResourceDTO resource : resources) {
            info = new PermissionInfo();
            info.setCode("test");
            info.setType(resource.getCategory());
            info.setUri(resource.getUri());
            info.setMethod(resource.getMethod());
            info.setName(resource.getName());
            //info.setMenu(resource.getMenuId());
            result.add(info);
        }
    }
}
